package link;

import array.ListNode;

public class RotateList61 {
    public ListNode rotateRight(ListNode head, int k) {
        if(head==null){
            return head;
        }
        int length = 0;
        ListNode l = head;
        while (l!=null){
            l=l.next;
            length++;
        }
        k = k%length;

        ListNode pre = new ListNode();
        pre.next = head;
        ListNode quick = pre;
        ListNode slow = pre;
        while (quick.next!=null && k--!=0){
            quick = quick.next;

        }
        while (quick.next!=null){
            quick = quick.next;
            slow = slow.next;
        }
        quick.next=head;
        ListNode temp = slow.next;
        slow.next = null;
        head = temp;
        return head;
    }
}
